package com.loan.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.ResponseResult;
import com.loan.entity.User;
import com.loan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户接口
 *
 * @author FBF
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户账号及时校验
     * FBF
     *
     * @param username
     * @return
     */
    @PostMapping("/username")
    @ResponseBody
    public ResponseResult username(String username) {
        if (username != null && username.trim().length() != 0) {
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.like("username", username);
            List<User> list = userService.list(queryWrapper);
            if (list.size() == 0) {
                return new ResponseResult<>(200, "账号可以使用");
            } else {
                return new ResponseResult<>(500, "账号已存在");
            }
        }
        return new ResponseResult<>(501, "账号不能为空");

    }

    /**
     * 用户注册
     * FBF
     *
     * @return
     */
    @GetMapping("/user_register")
    public String register() {
        return "user_register";
    }

    /**
     * 用户注册
     * FBF
     *
     * @param username
     * @param password
     * @return
     */
    //TODO 用户注册
    @PostMapping("/user_register")
    @ResponseBody
    public ResponseResult register(User user,String confirm) {
        if (confirm == null || confirm.trim().length()==0){
            return new ResponseResult<>(503, "请输入确认密码");
        }
        if (user.getUsername() == null || user.getUsername().trim().length()==0) {
            return new ResponseResult(501, "账号不能为空");
        }

        if (user.getPassword() == null || user.getPassword().trim().length()==0) {
            return new ResponseResult(502, "密码不能为空");
        }
        if (!user.getPassword().equals(confirm)){
            return new ResponseResult<>(504, "两次输入密码不一致");
        }
        boolean result = userService.save(user);
        if (result) {
            return new ResponseResult(200, "注册成功");
        }
        return new ResponseResult(503, "系统正在维护中请稍后再试");
    }

    /**
     * FBF
     *
     * @return
     */
    @GetMapping("/user_login")
    public String login() {
        return "user_login";
    }

    /**
     * FBF
     *
     * @param account
     * @param password
     * @param session
     * @return
     * @FBF 用户登录
     */
    @PostMapping("/user_login")
    @ResponseBody
    public ResponseResult<Void> login2(String username, String password, HttpSession session) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        List<User> list = userService.list(queryWrapper);
        if (list.size() == 0) {
            return new ResponseResult<>(501, "用户不存在");
        }
        if (!(list.get(0).getPassword().equals(password))) {
            return new ResponseResult<>(502, "密码错误");
        }
        session.setAttribute("username", username);
        return new ResponseResult<>(200, "登陆成功");
    }

    /**
     * FBF
     * 用户实名认证
     * @param user
     * @return
     */
    @PostMapping("/user_alter")
    @ResponseBody
    public ResponseResult<Void> userAlter(User user, HttpSession session) {
        String username = (String) session.getAttribute("username");
        user.setUsername(username);
        int result = userService.updateUser(user);
        if (result == 1) {
            return new ResponseResult<>(200, "认证成功");
        } else if (result == 0) {
            return new ResponseResult<>(500, "系统正在维护中请稍后再试...");
        }
        return new ResponseResult<>(501, "认证失败");
    }
}
